package com.nanyang.tms.controller.product;

import javax.validation.Valid;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.product.ProductQueryRequest;
import com.nanyang.tms.api.dto.request.product.ProductUpdateStateRequest;
import com.nanyang.tms.api.dto.request.product.add.ProductBaseAddRequest;
import com.nanyang.tms.api.dto.request.product.update.ProductBaseUpdateRequest;
import com.nanyang.tms.api.dto.response.product.ProductBaseResponse;
import com.nanyang.tms.api.dto.response.product.ProductPageKeyResponse;
import com.nanyang.tms.api.dto.response.product.ProductPageResponse;
import com.nanyang.tms.api.service.product.ProductRpcService;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private ProductRpcService productRpcService;

    /**
     * 列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<ProductPageResponse>> listProduct(@RequestBody ProductQueryRequest param) {
        ApiResult<PageBean<ProductPageResponse>> apiResult = productRpcService.listProduct(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 列表
     * @param param
     * @return
     */
    @RequestMapping(value = "/listByKey", method = RequestMethod.POST)
    public ApiResult<PageBean<ProductPageKeyResponse>> listProductByKey(@RequestBody ProductQueryRequest param) {
        ApiResult<PageBean<ProductPageKeyResponse>> apiResult = productRpcService.listProductByKey(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 添加
     * @param param
     * @return
     */

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveProduct(@BindParam @RequestBody @Valid ProductBaseAddRequest param) {
        ApiResult<Boolean> apiResult = productRpcService.saveProduct(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 修改
     * @param param
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveProduct(@BindParam @RequestBody @Valid ProductBaseUpdateRequest param) {
        ApiResult<Boolean> apiResult = productRpcService.updateProduct(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 禁用
     * @param param
     * @return
     */
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid ProductUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = productRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid ProductUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = productRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     *
     * <p>Description:查询根据编码</p>
     *
     * @param productCode
     * @return
     */
    @RequestMapping(value = "/selectByCode/{productCode}", method = RequestMethod.GET)
    public ApiResult<ProductBaseResponse> selectByCode(@PathVariable("productCode") String productCode) {
        ApiResult<ProductBaseResponse> apiResult = productRpcService.selectByCode(productCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
}
